package com.bootdo.wx.controller;

import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.HashMap;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.CmsWeContactDO;
import com.bootdo.wx.service.CmsWeContactService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;
/**
 * 
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2020-07-14 15:37:38
 */
 
@Controller
@RequestMapping("/wx/cmsWeContact")
public class CmsWeContactController {
	@Autowired
	private CmsWeContactService cmsWeContactService;
	
	@GetMapping()
	@RequiresPermissions("wx:cmsWeContact:cmsWeContact")
	String CmsWeContact(){
	    return "wx/cmsWeContact/cmsWeContact";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:cmsWeContact:cmsWeContact")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<CmsWeContactDO> cmsWeContactList = cmsWeContactService.likeList(query);
            int total = cmsWeContactList.size();
            PageUtils pageUtils = new PageUtils(cmsWeContactList, total);
            return pageUtils;
		}

		List<CmsWeContactDO> cmsWeContactList = cmsWeContactService.list(query);
		int total = cmsWeContactService.count(query);
		PageUtils pageUtils = new PageUtils(cmsWeContactList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:cmsWeContact:add")
	String add(){
	    return "wx/cmsWeContact/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:cmsWeContact:edit")
	String edit(@PathVariable("id") String id,Model model){
		CmsWeContactDO cmsWeContact = cmsWeContactService.get(id);
		model.addAttribute("cmsWeContact", cmsWeContact);
	    return "wx/cmsWeContact/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:cmsWeContact:add")
	public R save( CmsWeContactDO cmsWeContact){
		Map<String, Object> map = new HashMap<>();
		map.put("delFlag", "0");
		List<CmsWeContactDO> list = cmsWeContactService.list(map);
		if(list.size()>0){
			return R.error("只能添加一个");
		}
	    cmsWeContact.setId(UuidUtil.get32UUID());
	    cmsWeContact.setDelFlag(0);
	    cmsWeContact.setCaAt(new Date());
		if(cmsWeContactService.save(cmsWeContact)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:cmsWeContact:edit")
	public R update( CmsWeContactDO cmsWeContact){
		cmsWeContact.setUpAt(new Date());
		cmsWeContactService.update(cmsWeContact);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:cmsWeContact:remove")
	public R remove( String id){
		CmsWeContactDO cmsWeContact = cmsWeContactService.get(id);
		cmsWeContact.setDelFlag(1);
		cmsWeContact.setUpAt(new Date());
		if(cmsWeContactService.update(cmsWeContact)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:cmsWeContact:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				CmsWeContactDO cmsWeContact = cmsWeContactService.get(string);
				cmsWeContact.setDelFlag(1);
				cmsWeContact.setUpAt(new Date());
				cmsWeContactService.update(cmsWeContact);
			}
		}
	
		return R.ok();
	}
	
}
